package cn.szabadsag.hrm.controller;

import cn.szabadsag.hrm.doc.CourseDoc;
import cn.szabadsag.hrm.dto.CourseParamDto;
import cn.szabadsag.hrm.service.ICourseElasticsearchService;
import cn.szabadsag.hrm.util.AjaxResult;
import cn.szabadsag.hrm.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author yangcong
 * @date 2022年04月10日 21:20
 * @since
 */
@RestController
@RequestMapping("/course")
public class CourseESController {

    @Autowired
    private ICourseElasticsearchService courseElasticsearchService;

    /**
     * 课程查询
     * @param dto:查询的参数
     * @return
     */
    @PostMapping("/searchCourse")
    public AjaxResult searchCourse(@RequestBody CourseParamDto dto){
        PageList<CourseDoc> pageList = courseElasticsearchService.searchCourse(dto);
        return AjaxResult.me().setSuccess(true).setResultObj(pageList);
    }

    /**
     * 课程上线同步到ES
     * @param doc
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('es:saveCourse')")
    public AjaxResult add(@RequestBody CourseDoc doc){
        courseElasticsearchService.add(doc);
        return AjaxResult.me().setSuccess(true);
    }

    /**
     * 课程下线同步到ES
     * @param id:需要下线的课程
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        courseElasticsearchService.delete(id);
        return AjaxResult.me().setSuccess(true);
    }

}
